;*******************************************************************************
;   CS/ECEn 124 Lab 2 - blinky.asm: Software Toggle P1.0
;	Written by David Tanner
;	Started 4/30/2011
;   Finished 5/2/2011
;   MCLK = 13984 cycles per second
;   CPI  = 1.33  Cycles per instruction
;   MIPS = 10488
;   
;   
;
;   Description: Toggle P1.0 on and off every 10 seconds.
;
;             MSP430F2013/F2274
;             -----------------
;         /|\|              XIN|-
;          | |                 |
;          --|RST          XOUT|-
;            |                 |
;            |             P1.0|-->GREEN LED
;
;*******************************************************************************
            .cdecls C,LIST, "msp430x20x3.h" ; MSP430F2013
delay1      .equ    34858                   ; Set 1/10th of a second delay
delay2      .equ    100                     ; Set the 100 count delay 
initpoint   .equ    640                     ; Stack pointer constant
ledvalue    .equ    1                       ; Bit for the LED

;------------------------------------------------------------------------------
            .text                           ; beginning of executable code
;------------------------------------------------------------------------------


RESET:      mov.w   #initpoint,SP           ; init stack pointer, 5 cycles, 3 ins
            mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; stop WDT, 5 cycles, 3 ins
            bis.b   #ledvalue,&P1DIR        ; set P1.0 as output, 5 cycles, 2 ins
            

mainloop:   bis.b   #ledvalue,&P1OUT        ; toggle P1.0 led on, 5 cycles, 2 ins
            mov.w   #delay2,r15             ; use R15 as delay counter, 2 cycles, 2 ins
            jmp     flashloop               ; Flash the light back off quickly, 1 cycles

outerloop:  dec.w   r15                     ; delay over? decrement by 1, 2 cycles, 2 ins
              jnz   innerloop               ; n, 2 cycles, 1 ins
            jmp     mainloop                ; y, toggle led, 2 cycles, 1 ins
            
            
innerloop:  dec.w   r14                     ; delay over? decrement by 1, 2 cycles , 2 ins
			  jnz   innerloop               ; n repeat loop, 2 cycles, 1 ins
			mov.w   #delay1,r14			    ; Reset timer, 2 cycles, 2 ins
			jmp     outerloop               ; go back to outer loop, 2 cycles, 1 ins
			
			
flashloop:  dec.w   r14                     ; delay over? decrement by 1, 2 cycles, 2 ins
			  jnz   flashloop               ; n repeat loop, 2 cycles, 1 ins
			mov.w   #delay1,r14		        ; Reset timer, 2 cycles, 2 ins
			bic.b   #ledvalue,&P1OUT        ; Toggle led off, 5 cycles, 2 ins
			jmp     outerloop               ; Go to outer loop, 2 cycles, 1 ins

;------------------------------------------------------------------------------
;           Interrupt Vectors
;------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .short  RESET                   ; start address
            .end